#include "view_details.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "view_details.inc"
#include "app_trace.h"
#include "app_timer.h"
#include "times/view_time_0.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)

static void show_steps_details_percent(int x, int y, int width, int height, int percent, int color, uint8_t const *head_icon, uint8_t const *tail_icon)
{
    if (percent <= 0)
    {
        percent = 0;
    }
    else if (percent > 100)
    {
        percent = 100;
    }
#if 0
#warning debug
    percent = 87;
#endif
    width = percent * width / 100;
    if (width < 1)
    {
        return;
    }
    int head_width = head_icon[0] + (head_icon[1] << 8);
    int tail_width = tail_icon[0] + (tail_icon[1] << 8);
    int x_base = 0;
    int x_tail_base = 0;
    int x_tail = 0;
    if (width <= head_width)
    {
        x_base = x + width;
        disp_draw_user_image_less_x(x, y, head_icon, x_base);
    }
    else if (width <= head_width + tail_width)
    {
        disp_draw_user_image(x, y, head_icon);
        x_base = x + tail_width;
        x_tail = x_base;
        x_tail_base = x + width;

        disp_draw_user_image_less_x(x_tail, y, tail_icon, x_tail_base);
    }
    else
    {
        disp_draw_user_image(x, y, head_icon);
        int rect_width = width - head_width - tail_width;
        disp_draw_rect_color(x + head_width, y, x + head_width + rect_width - 1, y + height - 1, color);
        disp_draw_user_image(x + head_width + rect_width, y, tail_icon);
    }
}
static void show_steps_details_icon(void)
{
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 4, 46 - 9, FILE_IMG_step_detail_icon_4_46);
}
static void show_steps_details_back(void)
{
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 50, 64, FILE_IMG_B105_step_deep_50_64);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 50, 138, FILE_IMG_B107_kcal_deep_50_138);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_3, 50, 211, FILE_IMG_B109_distance_deep_50_211);
}
static void show_steps_details_steps(void)
{
    int x = disp_draw_user_image_num(55, 36, disp_ctrl.com_data.i_temp[5], 13, img_j301_TAB);
    disp_draw_user_image(x + 4, 43, img_common_step);

    int percent = disp_ctrl.com_data.i_temp[5] * 100 / system_config_get_target_step();
    show_steps_details_percent(50, 64, 186, 10, percent, 0x15f2, img_step_head, img_step_tail);
}
static void show_steps_details_calorie(void)
{
    int x = disp_draw_user_image_num(55, 110, disp_ctrl.com_data.i_temp[6], 13, img_j301_TAB);
    disp_draw_user_image(x + 4, 117, img_common_kcal);

    int percent = disp_ctrl.com_data.i_temp[6] * 100 / system_config_get_target_calorie();
    show_steps_details_percent(50, 138, 186, 10, percent, 0xf4ca, img_calorie_head, img_calorie_tail);
}
static void show_steps_details_distance(void)
{
    int x = disp_draw_user_image_num(55, 183, disp_ctrl.com_data.i_temp[7] / 1000, 13, img_j301_TAB);

    disp_draw_user_image(x, 183, img_j301_point);
    x += 5 + 3;
    x = disp_draw_user_image_num_with_digits(x, 183, disp_ctrl.com_data.i_temp[7] % 1000 / 10, 2, 13, img_j301_TAB);

    if (disp_ctrl.com_data.i_temp[2])
    {
        disp_draw_user_image(x + 4, 190, img_common_km);
    }
    else
    {
        disp_draw_user_image(x + 4, 190, img_common_mi);
    }

    int percent = disp_ctrl.com_data.i_temp[10] * 100 / system_config_get_target_dist();
    show_steps_details_percent(50, 211, 186, 10, percent, 0x563e, img_distance_head, img_distance_tail);
}

void view_details_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[5] = sport_get_present_step();
        disp_ctrl.com_data.i_temp[6] = sport_get_calorie();
        disp_ctrl.com_data.i_temp[10] = sport_get_distance();
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[7] = sport_get_distance() * 0.6213712;
            disp_ctrl.com_data.i_temp[2] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[7] = sport_get_distance();
            disp_ctrl.com_data.i_temp[2] = 1;
        }
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[0] = ble_if_connected();
        disp_ctrl.auto_refresh_time = 500;
    }
    if (disp_ctrl.com_data.i_temp[0])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    show_steps_details_icon();
    show_steps_details_back();
    show_steps_details_steps();
    show_steps_details_calorie();
    show_steps_details_distance();
}
view_event_result_t view_details_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
